package Redis缓存数据;

import cn.tedu.autoshow.commons.constants.JwtCacheConsts;
import cn.tedu.autoshow.commons.pojo.po.LoginInfoPO;
import cn.tedu.autoshow.commons.security.LoginPrincipal;
import cn.tedu.autoshow.passport.repository.IAdminJwtRepository;
import cn.tedu.autoshow.passport.security.AdminDetails;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.stereotype.Repository;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

@Repository
public class AdminJwtRepositoryImpl implements IAdminJwtRepository, JwtCacheConsts {

    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;



    @Override
    public void saveLoginInfo(String jwt, LoginInfoPO loginInfoPO, AdminDetails adminDetails) {
        String userTokenKey = LOGIN_HASH_ADMIN_PREFIX+loginInfoPO.getId();
        Collection<GrantedAuthority> authorities = adminDetails.getAuthorities();
        String authorityListJsonString = JSON.toJSONString(authorities);
        // 需要缓存到Redis中的数据
        HashOperations<String, Object, Object> operations = redisTemplate.opsForHash();
        //删除旧的jwt为key存储的登录信息
        String oldJwt = (String)operations.get(userTokenKey, ADMIN_HASH_WEB_JWT_PREFIX);
        if(oldJwt != null){operations.delete(userTokenKey, oldJwt);}
        //登录端
        operations.put(userTokenKey,ADMIN_HASH_WEB_JWT_PREFIX,jwt);
        //登录信息
        operations.put(userTokenKey,jwt,loginInfoPO);
        operations.put(userTokenKey,"enable",adminDetails.getEnable());
        operations.put(userTokenKey,"authorities",authorityListJsonString);
        //operations.put(userTokenKey,"expiredTime",tokenDuration);
    }

    @Override
    public LoginInfoPO getLoginInfo(String jwt, LoginInfoPO loginSimpleInfo) {
        HashOperations<String, Object, Object> operations = redisTemplate.opsForHash();
        String userTokenKey = LOGIN_HASH_ADMIN_PREFIX+loginSimpleInfo.getId();

        LoginInfoPO loginInfoPO = (LoginInfoPO) operations.get(userTokenKey, jwt);
        if(loginInfoPO == null){
            return null;
        }
        Integer enabled = (Integer) operations.get(userTokenKey, "enable");
        String authorityListJsonString = (String) operations.get(userTokenKey, "authorities");

        List<SimpleGrantedAuthority> authorities = JSON.parseArray(authorityListJsonString, SimpleGrantedAuthority.class);
        loginInfoPO.setEnable(enabled).setAuthorities(authorities);

        return loginInfoPO;
    }

    @Override
    public Boolean delete(String jwt, LoginPrincipal loginPrincipal) {
        HashOperations<String, Object, Object> operations = redisTemplate.opsForHash();
        String userTokenKey = LOGIN_HASH_ADMIN_PREFIX+loginPrincipal.getId();
        boolean flag = operations.hasKey(userTokenKey, jwt);
        if(flag){operations.delete(userTokenKey, jwt);}
        return flag;
    }

    @Override
    public void setLoginEnable(Long id, Integer enable) {
        String userTokenKey = LOGIN_HASH_ADMIN_PREFIX+id;
        // 需要缓存到Redis中的数据
        HashOperations<String, Object, Object> operations = redisTemplate.opsForHash();
        operations.put(userTokenKey,"enable",enable);
    }

}
